package org.taobao.ibatiside.ui.binding;

import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.taobao.ibatiside.model.mapperv2.SqlMap;
import org.taobao.ibatiside.ui.util.MiscHelper;
import org.taobao.ibatiside.ui.wizard.BatisSettingModel;

public class UniqueValidator implements IValidator {

	@Override
	public IStatus validate(Object value) {
		if (value == null || !(value instanceof String && ((String)value).length() > 0) ){
			return ValidationStatus.error("not a valid name of statement");
		}
		SqlMap sqlmap = BatisSettingModel.getActiveModel().getSqlmap();
		if (sqlmap != null && MiscHelper.getStatementById(sqlmap, ((String)value)) != null){
			return ValidationStatus.error("there has already existed a statement "+value+" in sqlmap file");
		}
		return ValidationStatus.OK_STATUS;
	}

}
